﻿using System.Linq;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;

namespace NazTek.Documenter.Ssis.Control.Tasks
{
    internal sealed class SsisPipe : SsisTaskBase
    {
        public override string Documentation
        {
            get
            {
                var subDoc = new StringBuilder();
                SubComponents.ForEach(c => subDoc.AppendLine(c.Documentation));
                var doc = new StringBuilder(string.Format(
                    "<li>{0} [{1}]<br><ul>{2}</ul></li>",
                    Name, Description, subDoc));
                return doc.ToString();
            }
        }

        internal SsisPackage Parent { get; set; }
        internal IDTSPath100[] Paths { get; private set; }
        internal SsisPipe(TaskHost component, SsisPackage parent)
        {
            Parent = parent;
            Name = component.Name;
            Description = component.Description;
            var pipe = (IDTSPipeline100) component.InnerObject;
            Paths = pipe.PathCollection.Cast<IDTSPath100>().ToArray();
            SubComponents = new PipeSubComponentList(
                pipe.ComponentMetaDataCollection.OfType<IDTSComponentMetaData100>().ToList(), this);
        }

        private PipeSubComponentList SubComponents { get; set; }
    }
}